package com.pg.auth.controller;

import com.alibaba.fastjson.JSON;
import com.pg.auth.model.dto.sysrole.SysRoleDTO;
import com.pg.auth.model.dto.sysrole.SysRoleReq;
import com.pg.auth.model.dto.sysrole.SysRoleVO;
import com.pg.auth.service.SysRoleService;
import com.pg.base.component.IdDTO;
import com.pg.base.component.PageVO;
import com.pg.base.component.Pager;
import com.pg.base.component.valid.Add;
import com.pg.base.component.valid.Update;
import com.pg.base.util.LoginUtils;
import com.pg.base.vo.ResultVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 * @author : huhuaping
 * @Date : 2024-06-06 19:55:33
 */

@Slf4j
@RestController
@RequestMapping("/sysRole")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     *  分页查询
     * @param pageReq 查询条件
     * @return PageVO  分页对象
     */
    @PostMapping("page")
    public ResultVO<PageVO<SysRoleVO>> page(@RequestBody Pager<SysRoleReq, SysRoleVO> pageReq) {

        log.info("receive SysRole page data=[{}]", JSON.toJSONString(pageReq));

        PageVO<SysRoleVO> pageVO = sysRoleService.page(pageReq);
        return ResultVO.instance(pageVO);
    }

    /**
     *  集合查询
     * @param sysRoleReq 查询条件
     * @return list  集合
     */
    @PostMapping("list")
    public ResultVO<List<SysRoleVO>> list(@RequestBody SysRoleReq sysRoleReq) {

        log.info("receive SysRole list data=[{}]", JSON.toJSONString(sysRoleReq));

        List<SysRoleVO> list = sysRoleService.list(sysRoleReq);

        return ResultVO.instance(list);
    }


    /**
     * 根据ID查询
     * @param roleId 主键ID
     * @return 记录对象
     */
    @GetMapping("get")
    public ResultVO<SysRoleVO> get(@RequestParam(required = true) Long roleId) {

        log.info("receive SysRole get data=[{}]", roleId);

        SysRoleVO sysRoleVO = sysRoleService.getById(roleId);

        return ResultVO.instance(sysRoleVO);
    }

    /**
     * 新增
     * @param sysRoleDTO  新增记录对象
     * @return  返回新增记录ID
     */
    @PostMapping("add")
    public ResultVO<Long> add(@RequestBody @Validated({Add.class}) SysRoleDTO sysRoleDTO) {

        log.info("receive SysRole add data=[{}]", JSON.toJSONString(sysRoleDTO));

        Long id = sysRoleService.add(sysRoleDTO,LoginUtils.getLoginUser());

        return ResultVO.instance(id);

    }


    /**
     * 根据id修改
     * @param sysRoleDTO  修改记录对象
     * @return 修改条数
     */
    @PostMapping("update")
    public ResultVO<Integer> update(@RequestBody @Validated({Update.class}) SysRoleDTO sysRoleDTO) {
        log.info("receive SysRole update data=[{}]", JSON.toJSONString(sysRoleDTO));

        Integer num = sysRoleService.updateById(sysRoleDTO,LoginUtils.getLoginUser());
        return ResultVO.instance(num);
    }


    /**
     * 删除记录
     * @param idDTO  删除ID对象
     * @return 删除条数
     */
    @PostMapping("delete")
    public ResultVO<Integer> delete(@RequestBody @Validated IdDTO idDTO) {
        log.info("receive SysRole delete data=[{}]", JSON.toJSONString(idDTO));
        Integer num =  sysRoleService.deleteById(idDTO.getId());
        return ResultVO.instance(num);

    }

}
